var http = require('http')
var shell = require('shelljs');


function init (){
  shell.exec('docker-compose down')
  shell.cd(__dirname)//##
  shell.cd('..')
  shell.exec('git pull')
  shell.cd('view')
  shell.exec('cnpm install')
  shell.exec('cnpm run build')
  //shell.cd('..')
  //shell.cd('manager')
  //shell.exec('cnpm run build')
  shell.cd('..')
  shell.exec('docker-compose stop')
  shell.exec('docker-compose rm')
  shell.exec('docker-compose up --force-recreate --build',function (a,b,c){
  //  console.log(a)2
  //  console.log(b)
  //  console.log(c)
  })
  shell.cd('webhook')
  console.log('部署成功，请等待')
}
//2222
http.createServer(function (req, res) {
  // 这里只是简单判断了 请求事件为Push Hook， x-gitee-token 在这里对应的是你git上配置webhook的密码
  console.log(req.headers)
  if (req.headers['x-gitee-event'] === 'Push Hook' && req.headers['x-gitee-token'] === 'Aa!@118086') {
    console.log('执行代码')
    init()
  }else {
    console.log('非法访问')
  }
}).listen(7777,()=> {
  console.log('开始监听')
})
